package com.example.newstest.until;


import android.os.AsyncTask;

import com.example.newstest.callback.OnCommentDataSuccessListener;
import com.example.newstest.comment.bean.CommentEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/17.
 */
public class TaskCommentData extends AsyncTask<String,Integer,List<CommentEntity>> {
   private OnCommentDataSuccessListener listener;

    public TaskCommentData(OnCommentDataSuccessListener listener) {
        this.listener = listener;
    }

    @Override
    protected List<CommentEntity> doInBackground(String... params) {
        String url = params[0];
        String json = MyHttpUtils.getStringFromUrl(url);
        CommentEntity commentEntity = null;
        List<CommentEntity> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            JSONArray array = object.getJSONArray("All");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object1 = array.getJSONObject(i);
                String  simtitle= object1.getString("simtitle");
                String  id= object1.getString("id");
                String  content= object1.getString("content");
                String  rid= object1.getString("rid");
                String  username= object1.getString("username");
                String  support= object1.getString("support");
                String  oppose= object1.getString("oppose");
                String  userid= object1.getString("userid");
                String  postdate= object1.getString("postdate");
                commentEntity = new CommentEntity(simtitle,id,content,rid,username,support,oppose,userid,postdate);
                list.add(commentEntity);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    protected void onPostExecute(List<CommentEntity> commentEntities) {
        super.onPostExecute(commentEntities);
        listener.onCommentDataSuccess(commentEntities);
    }
}
